<?php
// Include database connection
require_once __DIR__ . '/../../config/database.php';

// Create database connection
try {
    $dsn = "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET;
    $conn = new PDO($dsn, DB_USERNAME, DB_PASSWORD);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo json_encode(['status' => 'error', 'message' => '数据库连接失败: ' . $e->getMessage()]);
    exit;
}

// Set headers
header('Content-Type: application/json');

// Get module ID
$module_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($module_id <= 0) {
    echo json_encode(['status' => 'error', 'message' => 'Invalid module ID']);
    exit;
}

try {
    $sql = "SELECT id, name, slug, description, introduction, color_hex, 
                  is_premium, is_active, display_order
            FROM km_cognitive_modules 
            WHERE id = ?";
    
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(1, $module_id, PDO::PARAM_INT);
    $stmt->execute();
    
    $module = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$module) {
        echo json_encode(['status' => 'error', 'message' => 'Module not found']);
        exit;
    }
    
    echo json_encode(['status' => 'success', 'data' => $module]);
    
} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => 'Database error: ' . $e->getMessage()]);
} 